<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>
<script src="${ctxStatic}/jquery/jquery.form.js"></script>
<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="drugBuyPlanUpdateDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/drug/buyPlanAudit/updatePlanIndex')">采购计划调整</a>
                        </li>
                    </ul>
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="searchForm" modelAttribute="drugBuyPlan" onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/drug/buyPlanAudit/updatePlanIndex');"
                                                   method="post" class="form-inline">
                                            <div class="form-group">
                                                <label class=" control-label">采购单号：</label>
                                                <form:select path="purchaseNumber" id="purchaseNumber" htmlEscape="false" class="form-control" data-parsley-required="true">
                                                    <form:option value=""></form:option>
                                                    <form:options items="${his_fns:getBuyPlanNum('0')}" itemLabel="purchaseNumber" itemValue="purchaseNumber" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <input id="btnSubmit" class="btn btn-primary" type="submit" value="查询" />
                                                <input id="addBtn" class="btn btn-info" type="button" onclick="addBuyPlanItem()" value="增加" />
                                                <input id="saveBtn" class="btn btn-primary" type="button" onclick="saveListForm()" value="保存" />
                                            </div>
                                        </form:form>
                                    </div>
                                </div>
                                <form id="saveListForm"  action="${ctx}/drug/buyPlanAudit/saveUdatePlan" method="post" class="form-inline">
                                <table id="contentTableU" class="table table-bordered table-striped mg-t datatable">
                                    <thead>
                                    <tr>
                                        <th hidden>jsonText</th>
                                        <th hidden>id</th>
                                        <th hidden>stateFlag</th>
                                        <th hidden>infoId</th>
                                        <th hidden>采购序号</th>
                                        <th hidden>采购单号</th>
                                        <th>药品编码</th>
                                        <th>药品名称</th>
                                        <th>包装数量</th>
                                        <th>包装规格</th>
                                        <th hidden>包装单位代码</th>
                                        <th>包装单位</th>
                                        <th hidden>剂型</th>
                                        <th hidden>厂家ID</th>
                                        <th>厂家</th>
                                        <th style="width: 25px;">进价</th>
                                        <th>计划数量</th>
                                        <th>计划金额</th>
                                        <th>申请者</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach items="${page.list}" var="drugBuyPlan" varStatus="idx">
                                    <tr xia="${idx.index}">
                                        <td hidden>${drugBuyPlan.id}<input type="hidden" id="id${idx.index}" name="drugBuyPlanList[${idx.index}].id" value="${drugBuyPlan.id}"/></td>
                                        <td hidden>${drugBuyPlan.stateFlag}<input type="hidden" name="drugBuyPlanList[${idx.index}].stateFlag" value="${drugBuyPlan.stateFlag}"/></td>
                                        <td hidden> ${drugBuyPlan.infoId}<input type="hidden" id="infoId${idx.index}" name="drugBuyPlanList[${idx.index}].infoId" value="${drugBuyPlan.infoId}"/></td>
                                        <td hidden> ${drugBuyPlan.purchaseOrder}<input type="hidden" id="purchaseOrder${idx.index}" name="drugBuyPlanList[${idx.index}].purchaseOrder"
                                                                                 value="${drugBuyPlan.purchaseOrder}"/></td>
                                        <td hidden> ${drugBuyPlan.purchaseNumber}<input type="hidden" id="purchaseNumber${idx.index}" name="drugBuyPlanList[${idx.index}].purchaseNumber"
                                                                                        value="${drugBuyPlan.purchaseNumber}"/></td>
                                        <td> ${drugBuyPlan.drugCode}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugCode" value="${drugBuyPlan.drugCode}" /></td>
                                        <td> ${drugBuyPlan.drugName}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugName" value="${drugBuyPlan.drugName}"/></td>
                                        <td> ${drugBuyPlan.drugStock.drugInfo.packageNum}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugStock.drugInfo.packageNum"
                                                                                                value="${drugBuyPlan.drugStock.drugInfo.packageNum}"/></td>
                                        <td> ${drugBuyPlan.packageSpec}<input type="hidden" name="drugBuyPlanList[${idx.index}].packageSpec" value="${drugBuyPlan.packageSpec}"/></td>
                                        <td hidden> ${drugBuyPlan.packageUnit}<input type="hidden" name="drugBuyPlanList[${idx.index}].packageUnit" value="${drugBuyPlan.packageUnit}"/></td>
                                        <td> ${fns:getDictLabel(drugBuyPlan.packageUnit,'SPEC_UNIT' ,'' )}</td>
                                        <td hidden>${drugBuyPlan.drugForm}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugForm" value="${drugBuyPlan.drugForm}" /></td>
                                        <td hidden> ${drugBuyPlan.vedorName}<input type="hidden" name="drugBuyPlanList[${idx.index}].vedorName" value="${drugBuyPlan.vedorName}"/></td>
                                        <td> ${his_fns:getSupplierById(drugBuyPlan.vedorName)}</td>
                                        <td ><input name="drugBuyPlanList[${idx.index}].costPrice" id="costPrice${idx.index}" value="${drugBuyPlan.costPrice}" class="form-control"
                                                   onchange="changeFun('costPrice','${idx.index}')" ></td>   <%--进价--%>
                                        <td><input id="selling${idx.index}" name="drugBuyPlanList[${idx.index}].plannedSelling"
                                                    value="${drugBuyPlan.plannedSelling}" onchange="changeFun('selling','${idx.index}')" class="form-control" width="20px"/></td>
                                        <td><input id="amount${idx.index}" name="drugBuyPlanList[${idx.index}].plannedAmount" class="form-control" width="20px"
                                                    value="${drugBuyPlan.plannedAmount}" readonly/></td>
                                        <td>${his_fns:getNameByUserId(drugBuyPlan.warehouseKeeper)}
                                         <input type="hidden" name="drugBuyPlanList[${idx.index}].warehouseKeeper" value="${drugBuyPlan.warehouseKeeper}"/></td>
                                        <td>
                                            <a href="#" class="applyBt btn btn-danger btn-xs" onclick="removeList(${idx.index},this)">删除</a>
                                        </td>
                                    </tr>
                                    </c:forEach>
                                </table>
                                 </form>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
        <%--右侧药品明细信息--%>
        <section class="main-content col-lg-4" style="display: none" id="drugPlanRightDiv">
            <div class="content-wrap" id="rigthCenterDiv">
            </div>
        </section>
    </section>
</div>
<script type="text/javascript">
    //弹出右边增加药品采购明细框
    function addBuyPlanItem() {
        $("#drugBuyPlanUpdateDiv").attr("class", "main-content col-lg-8");
        $("#drugPlanRightDiv").show('normal');
        return loadDiv('rigthCenterDiv', '${ctx}/drug/buyPlanAudit/updatePlanForm');
    }

    //保存
    function saveListForm(){
        $("#saveListForm").ajaxSubmit({
            success: function(data){
                if(data.code == 'success'){
                    toastr.success(data.data);
                    loadDiv('drugBuyPlanUpdateDiv','${ctx}/drug/buyPlanAudit/updatePlanIndex');
                }else{
                    toastr.error(data.data);
                }
            },
            error: function(data){
                toastr.error(data.data);
            }
        });
    }

    //根据输入的采购数量计算采购金额
    function changeFun(inputId,idx){
        if(inputId == 'selling'){  //改变采购数量
            var num = $.trim($("#" + inputId + idx).val());
            var price = $.trim($("#costPrice" + idx).val());
            $("#amount" + idx).val(Number(num) * Number(price));
        }
        if(inputId == 'costPrice'){  //改变进价
            var price = $.trim($("#" + inputId + idx).val());
            var num = $.trim($("#selling" + idx).val());
            $("#amount" + idx).val(Number(num) * Number(price));
        }
    }

    //删除
    function removeList(tbIdx,btn) {
        var purchaseNumber = $('#purchaseNumber' + tbIdx).val();
        if(tbIdx == Number($("#contentTableU tbody tr:last").attr('xia'))){   //删除的是最后一行

        }else{
            $("#contentTableU tbody tr").each(function (index, item) {    //删除的不是最后一行

            });
        }

        var id = $('#id' + tbIdx).val();
        if(null == id || $.trim(id) == '' || typeof(id) == 'undefined'){
            toastr.success('删除成功!');
            $(btn).parent().parent().remove();
            return ;
        }
        $.ajax({
            type: "POST",
            url: '${ctx}/drug/buyPlanAudit/delete?id=' + id,
            success: function (data) {
                if (data.code == "success") {
                    toastr.success(data.data);
                    $(btn).parent().parent().remove();
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }
</script>